# -*- coding: utf-8 -*-

def replace_char(old_string, char:str, index:int):
    if len(char) != 1:
        raise ValueError("can only replace one character")
    old_string = str(old_string)
    new_string = old_string[:index] + char + old_string[index+1:]
    return new_string

def print_difference(string1:str, string2:str):
    for letter in range(len(string1)):
        if string1[letter] != string2[letter]:
            print("Sequences differ at postion {}".format(letter + 1))
            print("protein change:{}".format(string1[letter])+" > {}".format(string2[letter]))
            break
    else:
        print("Strings are identical")

def translate_codon (cod:str) :
    tc = {"GCT":"A", "GCC":"A", "GCA":"A", "GCG":"A",
          "TGT":"C", "TGC":"C",
          "GAT":"D", "GAC":"D",
          "GAA":"E", "GAG":"E",
          "TTT":"F", "TTC":"F",
          "GGT":"G", "GGC":"G", "GGA":"G", "GGG":"G",
          "CAT":"H", "CAC":"H",
          "ATA":"I", "ATT":"I", "ATC":"I",
          "AAA":"K", "AAG":"K",
          "TTA":"L", "TTG":"L", "CTT":"L", "CTC":"L", "CTA":"L", "CTG":"L",
          "ATG":"M", 
          "AAT":"N", "AAC":"N",
          "CCT":"P", "CCC":"P", "CCA":"P", "CCG":"P",
          "CAA":"Q", "CAG":"Q",
          "CGT":"R", "CGC":"R", "CGA":"R", "CGG":"R", "AGA":"R", "AGG":"R",
          "TCT":"S", "TCC":"S", "TCA":"S", "TCG":"S", "AGT":"S", "AGC":"S",
          "ACT":"T", "ACC":"T", "ACA":"T", "ACG":"T",
          "GTT":"V", "GTC":"V", "GTA":"V", "GTG":"V",
          "TGG":"W",
          "TAT":"Y", "TAC":"Y",
          "TAA":"_", "TAG":"_", "TGA":"_"}
    return tc.get(cod,None)

def translate_seq(dna_seq:str,ini_pos = 0):
    """
    Translates a DNA sequence into an aminoacid sequence.
    """
    seqm = dna_seq.upper()
    seq_aa = ""
    for pos in range(ini_pos,len(seqm)-2,3):
        cod = seqm[pos:pos+3]
        aa = translate_codon(cod)
        if not aa:
            ValueError(f"error codon: {cod}")
        seq_aa += aa
    return seq_aa[:len(seq_aa)-1]

if __name__ == "__main__":
    seq_before = "ATGACGTCAGAGTTTGATCATGGCTCAGGACGAACGCTGGCGGCGTGCTTAACACATGCAAGTCGAACGATGAAGCTCCAGCTTGCTGGGGTGGATTGGCGAACGGGTGAGCACGTGAGTAACCTGCCCTTGACTCTGGGAGCGTTGGAAACGACGTCTAATACCGGATATGACGACCGATGGCATCATCTGGTTGTGGAAAGAATTTTGGTCAAGGATGGACTCGCGGCCTATCAGGTAGTTGGTGAGGTAATGGCTCACCAAGCCTACGACGGGCCGGCCGAGGGTGACCGGCCACACTGGGACGACACGGCCCAGACTCCTACGGGAGGCAGCAGTGGGGAATATTGCACAATGGGCGAAAGCCTGATGCAGCAACGCCGCGTGAGGGATGACGGCTAG"
    aa = translate_seq(seq_before)

    #204 A>C
    seq_after = replace_char(seq_before, 'C', 203)
    bb = translate_seq(seq_after)

    print_difference(aa,bb)
